package com.ynet.middleground.user.service.impl;

import com.google.common.base.Throwables;
import com.ynet.core.common.BaseReqObj;
import com.ynet.core.common.ServiceResult;
import com.ynet.core.exception.BusinessException;
import com.ynet.core.log.IfpLogger;
import com.ynet.middleground.user.bean.CloudPlatfromCitySearchReq;
import com.ynet.middleground.user.common.ErrorMessageConstant;
import com.ynet.middleground.user.dto.CloudPlatformCityDTO;
import com.ynet.middleground.user.dto.CloudPlatformProvinceDTO;
import com.ynet.middleground.user.model.CloudPlatformAsync;
import com.ynet.middleground.user.model.CloudPlatformModel;
import com.ynet.middleground.user.model.UserModel;
import com.ynet.middleground.user.service.CloudPlatformService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;

import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import java.util.ArrayList;
import java.util.List;

@Path("cloudPlatform")
@Produces({MediaType.APPLICATION_JSON + "; " + "charset=UTF-8", MediaType.TEXT_XML + "; " + "charset=UTF-8"})
@Api(value = "云平台服务实现类", tags = {"云平台服务实现类"}, hidden = true)
@Service(timeout = 6000, version = "0.0.1")
public class CloudPlatformServiceImpl implements CloudPlatformService, GenericService {
    @Autowired
    CloudPlatformModel cloudPlatformModel;

    @Autowired
    UserModel userModel;

    @Autowired
    CloudPlatformAsync cloudPlatformAsync;

    @POST
    @Path("search-all-province")
    @ApiOperation(value = "查询云平台所有省份信息", notes = "查询云平台所有省份信息")
    @Override
    public ServiceResult<List<CloudPlatformProvinceDTO>> searchAllCloudPlatFormProvince(BaseReqObj baseReqObj) {
        ServiceResult<List<CloudPlatformProvinceDTO>> resultService = new ServiceResult<>();
        List<CloudPlatformProvinceDTO> result = new ArrayList<>();
        try {
            result = cloudPlatformModel.searchAllProvince(baseReqObj);
        } catch (BusinessException be) {
            resultService.setError(be.getCode(), be.getMessage());
            return resultService;
        } catch (Exception e) {
            // 获取当前方法名
            IfpLogger.error(Thread.currentThread().getStackTrace()[1].getMethodName(), "case: {}",
                Throwables.getStackTraceAsString(e));
            resultService.setError(ErrorMessageConstant.ECUC0000_CODE, ErrorMessageConstant.ECUC0000_MESSAGE);
            return resultService;
        }
        resultService.setResult(result);
        return resultService;
    }

    @POST
    @Path("search-city")
    @ApiOperation(value = "根据省份代码查询云平台地市信息", notes = "根据省份代码查询云平台地市信息")
    @Override
    public ServiceResult<List<CloudPlatformCityDTO>> searchAllCloudPlatFormCity(CloudPlatfromCitySearchReq reqObj) {
        ServiceResult<List<CloudPlatformCityDTO>> resultService = new ServiceResult<>();
        List<CloudPlatformCityDTO> result = new ArrayList<>();
        try {
            result = cloudPlatformModel.searchCityByProvinceCode(reqObj);
        } catch (BusinessException be) {
            resultService.setError(be.getCode(), be.getMessage());
            return resultService;
        } catch (Exception e) {
            // 获取当前方法名
            IfpLogger.error(Thread.currentThread().getStackTrace()[1].getMethodName(), "case: {}",
                Throwables.getStackTraceAsString(e));
            resultService.setError(ErrorMessageConstant.ECUC0000_CODE, ErrorMessageConstant.ECUC0000_MESSAGE);
            return resultService;
        }
        resultService.setResult(result);
        return resultService;
    }

    /**
     * 异常数据同步云平台
     *
     * @return
     */
    @POST
    @Path("doCloudPlatformSync")
    @ApiOperation(value = "云平台异常数据同步", notes = "云平台异常数据同步")
    @Override
    public ServiceResult<Object> doCloudPlatformSync(BaseReqObj baseReqObj) {
        ServiceResult<Object> result = new ServiceResult<>();
        try {
            IfpLogger.info("云平台服务实现类", "执行中台失败记录数据同步到云平台更新开始");
            cloudPlatformAsync.doCloudPlatformSync();
            IfpLogger.info("云平台服务实现类", "执行中台失败记录数据同步到云平台更新结束");
        } catch (BusinessException be) {
            result.setError(be.getCode(), be.getMessage());
            return result;
        } catch (Exception e) {
            // 获取当前方法名
            IfpLogger.error(Thread.currentThread().getStackTrace()[1].getMethodName(), "case: {}",
                Throwables.getStackTraceAsString(e));
            result.setError(ErrorMessageConstant.ECUC0000_CODE, ErrorMessageConstant.ECUC0000_MESSAGE);
            return result;
        }
        return result;
    }
}
